//
//  YZDoctorBottomInfoView.m
//  kuaiyi
//
//  Created by WGP on 16/5/5.
//
//

#import "YZDoctorBottomInfoView.h"
#import <Masonry.h>
#import "YZScrollView.h"

@interface YZDoctorBottomInfoView ()<UIScrollViewDelegate>

@property(nonatomic,weak)UIButton *conditionBtn;
@property(nonatomic,weak)UIButton *briefIntrodocutionBtn;
@property(nonatomic,weak)UIButton *timeBtn;
@property(nonatomic,strong)UIButton *currentSelectedBtn;
@property(nonatomic,strong)UIImageView *firstLine;
@property(nonatomic,strong)UIImageView *secondLine;
@property(nonatomic,strong)UIImageView *lineView;
@property(nonatomic,strong)UIView *sliderView;


@end
@implementation YZDoctorBottomInfoView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *conditionBtn = [self buttonWithTitle:@"接诊条件" tag:0];
    self.conditionBtn = conditionBtn;
    
    self.firstLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"illness_img_line.png"]];
    [self addSubview:self.firstLine];
    
    UIButton *briefIntrodocutionBtn = [self buttonWithTitle:@"医生简介" tag:1];
    self.briefIntrodocutionBtn = briefIntrodocutionBtn;
    
    self.secondLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"illness_img_line.png"]];
    [self addSubview:self.secondLine];

    
    UIButton *timeBtn = [self buttonWithTitle:@"就诊时间" tag:2];
    self.timeBtn = timeBtn;
    
    self.lineView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"illness_img_line.png"]];
    [self addSubview:self.lineView];
    
    self.sliderView = [[UIView alloc] init];
    self.sliderView.backgroundColor = YZGreenColor;
    [self addSubview:self.sliderView];
    
    self.scrollView = [[YZScrollView alloc] init];
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width*3, 0);
    [self addSubview:self.scrollView];
    
    
    [briefIntrodocutionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self);
    }];
    
    [self.firstLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.conditionBtn);
        make.left.mas_equalTo([UIScreen mainScreen].bounds.size.width/3);
    }];
    
    [conditionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self).offset(30);
    }];
    
    [self.secondLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.briefIntrodocutionBtn);
        make.left.mas_equalTo([UIScreen mainScreen].bounds.size.width/3 * 2);
    }];
    
    [timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.right.equalTo(self).offset(-30);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.top.equalTo(self.secondLine.mas_bottom).offset(5);
        make.left.right.equalTo(self);
    }];
    
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(3);
        make.centerX.equalTo(self).offset(-[UIScreen mainScreen].bounds.size.width*0.333);
        make.centerY.equalTo(self.lineView);
    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.top.equalTo(self.lineView).offset(3);
    }];

    
    self.currentSelectedBtn = self.conditionBtn;
    self.conditionBtn.selected = YES;
    
}

-(void)setDoctorModel:(YZDoctorModel *)doctorModel
{
    _doctorModel = doctorModel;
   self.scrollView.doctorModel = self.doctorModel;
}

-(void)btnDidClick:(UIButton *)btn
{
    if (self.currentSelectedBtn == btn) {
        return ;
    }
    
    self.currentSelectedBtn.selected = NO;
    self.currentSelectedBtn = btn;
    self.currentSelectedBtn.selected = YES;
    
    [self moveSliderViewWith:btn.tag];
    [self.scrollView setContentOffset:CGPointMake(btn.tag*self.scrollView.bounds.size.width, 0) animated:YES];
}


-(void)moveSliderViewWith:(NSInteger)index
{
    if (index == 0) {
        
        [self.sliderView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).offset(-[UIScreen mainScreen].bounds.size.width*0.333);
        }];

    }else if (index == 1)
    {
        [self.sliderView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self);
        }];
    }else if(index == 2)
    {
        [self.sliderView mas_updateConstraints:^(MASConstraintMaker *make) {
             make.centerX.equalTo(self).offset([UIScreen mainScreen].bounds.size.width*0.333);
        }];
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
    
   
}

-(UIButton *)buttonWithTitle:(NSString *)title tag:(NSInteger)tag
{
    UIButton *btn = [[UIButton alloc] init];
    btn.tag = tag;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn sizeToFit];
    [self addSubview:btn];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:YZGreenColor forState:UIControlStateSelected];
    return btn;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = (scrollView.contentOffset.x+scrollView.bounds.size.width*0.5)/scrollView.bounds.size.width;
    UIButton *button = nil;
    if (index == 0) {
        button = self.conditionBtn;
    }
    else if (index == 1)
    {
        button = self.briefIntrodocutionBtn;
    }
    else if(index == 2)
    {
        button = self.timeBtn;
    }
    [self btnDidClick:button];
}

@end
































